package mumu.android.lib.view.webview;

import mumu.android.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 顶栏控件
 */
public class TitleBarView extends LinearLayout implements View.OnClickListener {
	private static final String TAG = "TitleBarView";
	private String mUrl;
	private int mBar;

	private View top_main;

	class G {
		ImageButton top_common_imgbtn_prev;
		TextView top_common_txt_center;
		ImageButton top_common_imgbtn_user;
	}

	G g = new G();

	void initG() {
		if (top_main != null && top_main.getVisibility() == View.VISIBLE) {
			g.top_common_imgbtn_prev = (ImageButton) top_main.findViewById(R.id_top.imgbtn_prev);
			g.top_common_imgbtn_user = (ImageButton) top_main.findViewById(R.id_top.imgbtn_user);
			g.top_common_txt_center = (TextView) top_main.findViewById(R.id_top.txt_center);
		}
	}

	private OnTopButtonClickListener mOnTopButtonClickListener;

	public TitleBarView(Context context) {
		super(context);
		init(null, 0);
		initLayout(context);
	}

	public TitleBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs, 0);
		initLayout(context);
	}

	private void init(AttributeSet attrs, int defStyle) {
		// Load attributes
		// final TypedArray a = getContext().obtainStyledAttributes(attrs,
		// R.styleable.TitleBarView, defStyle, 0);
		// mUrl = a.getString(R.styleable.TitleBarView_url);
		// a.recycle();
	}

	private void initLayout(Context context) {
		LayoutInflater factory = LayoutInflater.from(context);
		top_main = factory.inflate(R.layout.titlebar, this);
	}
	
	
	public String getUrl() {
		return mUrl;
	}
	
	public void setUrl(String url) {
		mUrl = url;
		// mBar = UrlHelper.getFragment(UrlHelper.getAct(url));
		switch (mBar) {
		// 返回 +文字
		case 11:
			top_main.setVisibility(View.GONE);
			initG();
			// g.top_common_txt_center.setText(UrlHelper.getTitle(url));
			g.top_common_imgbtn_user.setVisibility(View.GONE);
			g.top_common_imgbtn_prev.setOnClickListener(this);
			// g.top_common_imgbtn_prev.setImageResource(R.drawable.top_return);
			break;
		default:
			top_main.setVisibility(View.GONE);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// switch (v.getId()) {
		// case R.id_top.imgbtn_prev:
		// if (mBar == 10 || mBar == 11) {
		//
		// }
		// break;
		// case R.id_top.imgbtn_user:
		//
		// break;
		// case R.id_top.imgbtn_search:
		//
		// break;
		// default:
		// break;
		// }
		String url = "javascript:TopButtonClick(%s,'%s')";
		url = String.format(url, v.getTag(), "");
		Log.d(TAG, url);
		if (mOnTopButtonClickListener != null) {
			mOnTopButtonClickListener.onClick(url);
		}
	}

	public void setOnTopButtonClickListener(OnTopButtonClickListener mOnTopButtonClickListener) {
		this.mOnTopButtonClickListener = mOnTopButtonClickListener;
	}

	public interface OnTopButtonClickListener {

		void onClick(String url);
	}
}
